package com.cat.dataStructure03;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/check-if-array-pairs-are-divisible-by-k/
 * @create 2025/11/15 17:17
 * @since JDK17
 */

public class Solution59 {
    public boolean canArrange(int[] arr, int k) {
        int n = arr.length, ans = 0;
        Map<Integer, Integer> map = new HashMap<>();
        for (int x : arr) {
            int mod = k - (x % k + k) % k;
            int cnt = map.getOrDefault(mod == k ? 0 : mod, 0);
            if (cnt == 0) {
                map.merge((x % k + k) % k, 1, Integer::sum);
            } else {
                map.merge(mod == k ? 0 : mod, -1, Integer::sum);
                ans++;
            }
        }
        return ans == n / 2;
    }
}
